package geiwomisa

class RegisterController extends grails.plugins.springsecurity.ui.RegisterController {
	
	def index = {
		[command: new RegisterCommand()]
	}
	
	
	
	
		static final passwordValidator = { String password, command ->
			if (command.username && command.username.equals(password)) {
				return 'command.password.error.username'
			}
	
			if (password && password.length() >= 3 && password.length() <= 64 &&
					(!password.matches('^.*\\p{Alpha}.*$') ||
					!password.matches('^.*\\p{Digit}.*$') ||
					!password.matches('^.*[!@#$%^&].*$'))) {
				return 'command.password.error.strength'
			}
		}
		
		
}

class RegisterCommand {
	
	String username
	String email
	String password
	String password2
	
	static constraints = {
		username blank: false, validator: { value, command ->
		if (value) {
		def User = AH.application.getDomainClass(
														 SpringSecurityUtils.securityConfig.userLookup.userDomainClassName).clazz
				if (User.findByUsername(value)) {
					return 'registerCommand.username.unique'
				}
			}
	}
		email blank: false, email: true, validator: { value, command ->
				if (value) {
				def User = AH.application.getDomainClass(
														 SpringSecurityUtils.securityConfig.userLookup.userDomainClassName).clazz
				if (User.findByEmail(value)) {
					return 'registerCommand.email.unique'
		}
			}
	}
		password blank: false, minSize: 8, maxSize: 64, validator: RegisterController.myPasswordValidator
	password2 validator: RegisterController.password2Validator
	}
	}
	
	
	
	class ResetPasswordCommand {
	String username
	String password
	String password2
	
	static constraints = {
	password blank: false, minSize: 8, maxSize: 64, validator: RegisterController.myPasswordValidator
	password2 validator: RegisterController.password2Validator
	}
	}
	